package com.verisette.ncos.web.master.action;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import org.corp.agi.common.annotation.RequiresAuthentication;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import com.verisette.ncos.common.CommonUtil;
import com.verisette.ncos.common.PrivConst;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.CustomLogAuditLog;
import com.verisette.ncos.service.master.gateway.AuditLogService;

import com.verisette.ncos.web.master.bean.AuditLogSearchBean;

/**
 * {Description here}
 *
 * @author Thippawan Muangchu (Tip)
 * @since Apr 8, 2010 11:50:43 AM
 * @version 1.0.0
 * @see 
 */

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AuditLogAction extends BaseAction {
	private Logger logger = Logger.getLogger(AuditLogAction.class);
	
	// Search criteria
	private AuditLogSearchBean auditLogSearchBean;
	
	private List<CustomLogAuditLog> resultList;
	
	//--paging variable
	private PagingBean pagingBean;
	private String sortMode;
	
	//initial service
	private AuditLogService auditLogService;
	@Autowired
	@Required
	public void setAuditLogService(AuditLogService auditLogService) {
		this.auditLogService = auditLogService;
	}
	
	@RequiresAuthentication(granted={PrivConst.S040100})
	public String init() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": init" );
		
		//paging
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);

		// Set Default to fromDate to toDate
		auditLogSearchBean = new AuditLogSearchBean();
		Date currentDate = new Date();
		auditLogSearchBean.setFromDate(CommonUtil.date2StrFormat(currentDate, ProjectConstant.DATE_FORMAT));
		auditLogSearchBean.setToDate(CommonUtil.date2StrFormat(currentDate, ProjectConstant.DATE_FORMAT));
		
		return "searchAuditLog";
	}
	
	@RequiresAuthentication(granted={PrivConst.S040101})
	public String search() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": search" );
		
		ServiceRequest<AuditLogSearchBean> inquiryAuditLogReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		inquiryAuditLogReq.setValueObject(auditLogSearchBean);
		
		if(sortMode != null && !"".equals(sortMode))
			pagingBean.setOrderMode(sortMode);
		else
			sortMode = PagingBean.ORDER_ASC;
		
		pagingBean.setOrderBy("adlDateTime");
		inquiryAuditLogReq.setPaging(pagingBean);
		
		ServiceResponse<List<CustomLogAuditLog>> inquiryAuditLogRes = auditLogService.inquiryAuditLog(inquiryAuditLogReq);
		
		if(isServiceResponseSuccess(inquiryAuditLogRes)){
			resultList = inquiryAuditLogRes.getValueObject();
			
			// set uspStatus for show
			for(CustomLogAuditLog c : resultList){
				if(ProjectConstant.STATUS_ASSIGN.equals(c.getStatusOrAction())){
					//c.setStatus("Assign");
					c.setStatusOrAction("Assign");
					
				}else if(ProjectConstant.STATUS_EDIT.equals(c.getStatusOrAction())){
					//c.setStatus("Edit");
					c.setStatusOrAction("Edit");
					
				}else if(ProjectConstant.STATUS_WAIT_FOR_APPROVE.equals(c.getStatusOrAction())){
					//c.setStatus("Wait for Approve");
					c.setStatusOrAction("Wait for Approve");
					
				}else if(ProjectConstant.STATUS_APPROVE.equals(c.getStatusOrAction())){
					//c.setStatus("Approve");
					c.setStatusOrAction("Approve");
					
				}else if(ProjectConstant.STATUS_PENDING.equals(c.getStatusOrAction())){
					//c.setStatus("Pending");
					c.setStatusOrAction("Pending");
					
				}
				
			}
			
		}else{
			
			return "searchAuditLog";
			
		}
		
		return "searchAuditLog";
	}
	
	//getter-setter
	public PagingBean getPagingBean() {
		return pagingBean;
	}
	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public AuditLogSearchBean getAuditLogSearchBean() {
		return auditLogSearchBean;
	}

	public void setAuditLogSearchBean(AuditLogSearchBean auditLogSearchBean) {
		this.auditLogSearchBean = auditLogSearchBean;
	}

	public List<CustomLogAuditLog> getResultList() {
		return resultList;
	}

	public void setResultList(List<CustomLogAuditLog> resultList) {
		this.resultList = resultList;
	}

	public String getSortMode() {
		return sortMode;
	}

	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}
	
}
